<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Timeline extends CI_Controller {

	public function index() {
		
	}

	public function getEventData() {
		$this->load->model('Events_model');
		$cursor = $this->Events_model->getEventData();
		sleep(1);
		// Conver mongoCursor to JSON
		echo json_encode(iterator_to_array($cursor));				
	}
	public function getEventTime($region) {
		$this->load->model('Events_model');
		if($region == 'vn')
			$cursor = $this->Events_model->getVnTime();
		else $cursor = $this->Events_model->getWorldTime();
		// sleep(3);

		// Conver mongoCursor to JSON
		echo json_encode(iterator_to_array($cursor));		
	}

	public function getProvinces() {
		$arr = $this->input->post('ids');
		$arr = array_map('intval', $arr);
		$this->load->model('Events_model');
		$cursor = $this->Events_model->getProvinces($arr);
		// usleep(400000); // delay 0.4s

		// Conver mongoCursor to JSON
		echo json_encode(iterator_to_array($cursor));
	}
	public function getCountries() {
		$arr = $this->input->post('ids');
		$arr = array_map('intval', $arr);
		$this->load->model('Events_model');
		$cursor = $this->Events_model->getCountries($arr);
		// usleep(400000); // delay 0.4s

		// Conver mongoCursor to JSON
		echo json_encode(iterator_to_array($cursor));
	}

	public function getAllCountries() {
		$this->load->model('Events_model');
		$cursor = $this->Events_model->getAllCountries();
		$cursor->sort(array('name' => 1));
		$arr = array();
		foreach(iterator_to_array($cursor) as $cur) {
			array_push($arr, $cur);
		}
		echo json_encode($arr);
	}

	// =====================================================================
	// =====================================================================
	// =====================================================================
	public function getEventName($id) {
		$this->load->model('Events_model');
		$cursor = $this->Events_model->getEventName($id);

		// in model get findOne function so don't use "iterator_to_array"
		echo json_encode($cursor);
	}
}

?>